package vlms.servlet;

import java.io.IOException;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vlms.bean.IssuedMovie;
import vlms.bean.Movie;
import vlms.bean.Person;
import vlms.service.VLMSServiceProxy;

/**
 * Servlet implementation class HandleIssuedMovie
 */
public class HandleIssuedMovie extends HttpServlet {
	private static final long serialVersionUID = 1L;
	VLMSServiceProxy proxy = new VLMSServiceProxy(); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HandleIssuedMovie() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
		proxy.setEndpoint("http://localhost:8080/VLMS/services/VLMSService");
		
		HttpSession session = request.getSession(true);
		
		String personId=request.getParameter("hidden");
		String input=request.getParameter("submit");
		
		if(input.equals("Delete"))
		{
			Person person=proxy.retrievePersonById(personId);
			
			if(person.getMembertype().equals("SC"))
			{
				proxy.deleteSimpleCustomer(personId);
			}
			else if(person.getMembertype().equals("PC"))
			{
				proxy.deletePremiumCustomer(personId);
			}
			request.setAttribute("message", "Person successfully deleted;");
			
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/displayAllPerson.jsp");
			dispatcher.forward(request, response);
		}
		else
		{
		IssuedMovie[] issuedMovie=null;
		Movie[] movie=null;
		String result[]=proxy.retrieveIssuedMoviesByPersonId(personId);
		if(result!=null)
		{
			int size=result.length;
			movie=new Movie[size];
			issuedMovie=new IssuedMovie[size];
			
			int index=0;
			for(String temp: result)
			{
				StringTokenizer str=new StringTokenizer(temp,":");
				
				if(str.countTokens()==9)
				{
					System.out.println(str.countTokens());
					movie[index]=new Movie();
					issuedMovie[index]=new IssuedMovie();
					//String.valueOf(issuedMovie.getIssueid()) + ":" + String.valueOf(issuedMovie.getPersonid()) + ":" + movie.getMovieid() + ":" + movie.getMoviename() + ":" + movie.getCategory() + ":" 
					//+ String.valueOf(movie.getRentamount()) + ":" + String.valueOf(issuedMovie.getIssuedate()) + ":" + String.valueOf(issuedMovie.getDuedate()) + ":" 
					//+ String.valueOf(issuedMovie.getReturndate());
					issuedMovie[index].setIssueid(Integer.parseInt(str.nextToken()));
					str.nextToken();
					movie[index].setMovieid(Integer.parseInt(str.nextToken()));
					movie[index].setMoviename(str.nextToken());
					movie[index].setCategory(str.nextToken());
					movie[index].setRentamount(Float.parseFloat(str.nextToken()));
					Calendar  cal = Calendar.getInstance();
					cal.setTime(new SimpleDateFormat("yyyy-mm-dd").parse(str.nextToken()));
					issuedMovie[index].setIssuedate(cal);
					
					
					cal.setTime(new SimpleDateFormat("yyyy-mm-dd").parse(str.nextToken()));
					
					issuedMovie[index].setDuedate(cal);
					String date=str.nextToken();
					if(date==null)
					{
						issuedMovie[index].setIssuedstatus("CURRENT");
					}
					else
					{
						issuedMovie[index].setIssuedstatus("RETURNED");
					}
					index++;
				}
			}
		
			
		}
		if(movie!=null && issuedMovie!=null)
		{
			request.setAttribute("movieList", movie);
			request.setAttribute("issuedMovieList", issuedMovie);
			
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ListIssuedMovies.jsp");
			dispatcher.forward(request, response);
		}
		else
		{
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/displayAllPerson.jsp");
		dispatcher.forward(request, response);
		}
		}
		
		
	}
		catch(RemoteException re)
		{
			String message=re.getMessage();
			request.setAttribute("message", message);
			re.printStackTrace();
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/displayAllPerson.jsp");
			dispatcher.forward(request, response);
		}
		catch(Exception e)
		{
			String message=e.getMessage();
			request.setAttribute("message", message);
			e.printStackTrace();
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/displayAllPerson.jsp");
			dispatcher.forward(request, response);
		}
	}
}
